package com.learning.spring.cloud.ms.provider.user.security;

import com.google.common.collect.Lists;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * ClassName: UserDetailsServiceImpl
 * Description:
 * Date: 2019/6/4 17:18 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class UserDetailsServiceImpl implements UserDetailsService {

    /**
     * 模拟两个账户：
     * ① 账号是 user，密码是 user，角色是user-role
     * ② 账号是 admin，密码是 admin，角色是admin-role
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if ("user".equals(username)) {
            return new User("user", "user", Lists.newArrayList(new SimpleGrantedAuthority("user-role")));
        } else if ("admin".equals(username)) {
            return new User("admin", "admin", Lists.newArrayList(new SimpleGrantedAuthority("admin-role")));
        } else {
            return null;
        }
    }
}
